home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / geom_lib / bbox.c next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  5.2 KB  |  169 lines

  1. /******************************************************************************
  2. * Bbox.c - computes bounding boxes for objects.                      *
  3. *******************************************************************************
  4. * Written by Gershon Elber, June 1993.                          *
  5. ******************************************************************************/
  6.  
  7. #include "irit_sm.h"
  8. #include "allocate.h"
  9. #include "cagd_lib.h"
  10. #include "bbox.h"
  11.  
  12. #define RESET_BBOX(Bbox) { Bbox.Min[0] = Bbox.Min[1] = Bbox.Min[2] = INFINITY;\
  13.              Bbox.Max[0] = Bbox.Max[1] = Bbox.Max[2] = -INFINITY; }
  14.  
  15. #define SET_IF_LESS_THAN(Val, NewVal)     { if (NewVal < Val) Val = NewVal; }
  16. #define SET_IF_GREATER_THAN(Val, NewVal)  { if (NewVal > Val) Val = NewVal; }
  17. #define SET_PT_IF_LESS_THAN(Pt, NewPt)    { SET_IF_LESS_THAN(Pt[0], NewPt[0]) \
  18.                         SET_IF_LESS_THAN(Pt[1], NewPt[1]) \
  19.                         SET_IF_LESS_THAN(Pt[2], NewPt[2]) }
  20. #define SET_PT_IF_GREATER_THAN(Pt, NewPt) \
  21.                        { SET_IF_GREATER_THAN(Pt[0], NewPt[0]) \
  22.                      SET_IF_GREATER_THAN(Pt[1], NewPt[1]) \
  23.                      SET_IF_GREATER_THAN(Pt[2], NewPt[2]) }
  24.  
  25. /*****************************************************************************
  26. * Computes a bounding box of a given object of any type.             *
  27. *****************************************************************************/
  28. BBBboxStruct *BBComputeBboxObject(IPObjectStruct *PObj)
  29. {
  30.     static BBBboxStruct Bbox;
  31.     int i;
  32.     IPObjectStruct *PObjTmp;
  33.     BBBboxStruct *PBbox;
  34.     CagdBBoxStruct CagdBbox;
  35.  
  36.     switch (PObj -> ObjType) {
  37.     case IP_OBJ_POLY:
  38.         return BBComputePolyListBbox(PObj -> U.Pl);
  39.     case IP_OBJ_POINT:
  40.         return BBComputePointBbox(PObj -> U.Pt);
  41.     case IP_OBJ_VECTOR:
  42.         return BBComputePointBbox(PObj -> U.Vec);
  43.     case IP_OBJ_LIST_OBJ:
  44.         RESET_BBOX(Bbox);
  45.         PBbox = &Bbox;
  46.         for (i = 0; (PObjTmp = ListObjectGet(PObj, i++)) != NULL; )
  47.         PBbox = BBMergeBbox(PBbox, BBComputeBboxObject(PObjTmp));
  48.         return PBbox;
  49.     case IP_OBJ_CURVE:
  50.         CagdCrvListBBox(PObj -> U.Crvs, &CagdBbox);
  51.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  52.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));
  53.         return &Bbox;
  54.     case IP_OBJ_SURFACE:
  55.         CagdSrfListBBox(PObj -> U.Srfs, &CagdBbox);
  56.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  57.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));    
  58.         return &Bbox;
  59.     default:
  60.         RESET_BBOX(Bbox);
  61.         return &Bbox;
  62.     }
  63. }
  64.  
  65. /*****************************************************************************
  66. * Computes a bounding box of a given object list of any type.             *
  67. *****************************************************************************/
  68. BBBboxStruct *BBComputeBboxObjectList(IPObjectStruct *PObj)
  69. {
  70.     BBBboxStruct Bbox, *PBbox;
  71.  
  72.     RESET_BBOX(Bbox);
  73.     PBbox = &Bbox;
  74.  
  75.     for ( ; PObj != NULL; PObj = PObj -> Pnext) {
  76.     PBbox = BBMergeBbox(PBbox, BBComputeBboxObject(PObj));
  77.     }
  78.  
  79.     return PBbox;
  80. }
  81.  
  82. /******************************************************************************
  83. * Computes a bounding box around a single polygon.                  *
  84. ******************************************************************************/
  85. BBBboxStruct *BBComputeOnePolyBbox(IPPolygonStruct *PPoly)
  86. {
  87.     static BBBboxStruct Bbox;
  88.     IPVertexStruct
  89.     *V = PPoly -> PVertex;
  90.  
  91.     RESET_BBOX(Bbox);
  92.  
  93.     do {
  94.     SET_PT_IF_LESS_THAN(Bbox.Min, V -> Coord);
  95.     SET_PT_IF_GREATER_THAN(Bbox.Max, V -> Coord);
  96.     V = V -> Pnext;
  97.     }
  98.     while (V != NULL && V != PPoly -> PVertex);
  99.  
  100.     return &Bbox;
  101. }
  102.  
  103. /******************************************************************************
  104. * Computes a bounding box around a list of polygons.                  *
  105. ******************************************************************************/
  106. BBBboxStruct *BBComputePolyListBbox(IPPolygonStruct *PPoly)
  107. {
  108.     static BBBboxStruct Bbox;
  109.  
  110.     RESET_BBOX(Bbox);
  111.  
  112.     for ( ; PPoly != NULL; PPoly = PPoly -> Pnext) {
  113.     IPVertexStruct
  114.         *V = PPoly -> PVertex;
  115.  
  116.     do {
  117.         SET_PT_IF_LESS_THAN(Bbox.Min, V -> Coord);
  118.         SET_PT_IF_GREATER_THAN(Bbox.Max, V -> Coord);
  119.         V = V -> Pnext;
  120.     }
  121.     while (V != NULL && V != PPoly -> PVertex);
  122.     }
  123.  
  124.     return &Bbox;
  125. }
  126.  
  127. /******************************************************************************
  128. * Computes a bounding box around a point.                      *
  129. ******************************************************************************/
  130. BBBboxStruct *BBComputePointBbox(RealType *Pt)
  131. {
  132.     static BBBboxStruct Bbox;
  133.  
  134.     PT_COPY(Bbox.Min, Pt);
  135.     PT_COPY(Bbox.Max, Pt);
  136.  
  137.     return &Bbox;
  138. }
  139.  
  140. /******************************************************************************
  141. * Merge (union) two bounding boxes into one.                      *
  142. * Either Bbox1 or Bbox2 can be pointing to the static area Bbox.          *
  143. ******************************************************************************/
  144. BBBboxStruct *BBMergeBbox(BBBboxStruct *Bbox1, BBBboxStruct *Bbox2)
  145. {
  146.     static BBBboxStruct Bbox;
  147.     int i;
  148.  
  149.     /* Make sure first Bbox is in Bbox and Bbox2 holds the other one. */
  150.     if (Bbox1 == &Bbox) {
  151.     }
  152.     if (Bbox2 == &Bbox) {
  153.     Bbox2 = Bbox1;
  154.     }
  155.     else {
  156.     GEN_COPY(&Bbox, Bbox1, sizeof(BBBboxStruct));
  157.     }
  158.  
  159.     /* Compare the two Bbox's and update. */
  160.     for (i = 0; i < 3; i++) {
  161.     if (Bbox.Min[i] > Bbox2 -> Min[i])
  162.         Bbox.Min[i] = Bbox2 -> Min[i];
  163.     if (Bbox.Max[i] < Bbox2 -> Max[i])
  164.         Bbox.Max[i] = Bbox2 -> Max[i];
  165.     }
  166.  
  167.     return &Bbox;
  168. }
  169.